package br.com.comanda.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import br.com.comanda.restclient.RestClient;
import br.com.comanda.util.JSONToVO;
import br.com.comanda.util.VOToJSON;
import br.com.comanda.util.WebServiceUtil;
import br.com.comanda.vos.Account;
import br.com.comanda.vos.Item;

public class AccountService {

	public List<Account> readAllAccounts() {
		RestClient client = new RestClient();
		String result = client.getRESTFileContent(WebServiceUtil.URL + "accounts");
		List<Account> accountList = new ArrayList<Account>();
		JSONObject accountResult;
		try {
			accountResult = new JSONObject(result);
			JSONArray accountsJson = accountResult.getJSONArray("account");

			JSONObject accountJson = null;
			Account account = null;

			for (int i = 0; i < accountsJson.length(); i++) {
				accountJson = new JSONObject(accountsJson.getString(i));
				account = new Account(accountJson.getInt("id"), accountJson.getString("client"), JSONToVO.toBoard(accountJson.getString("board")));

				if (accountJson.has("items")) {
					account.setItems(new ArrayList<Item>());

					try {

						JSONArray itemsJSON = new JSONArray(accountJson.getString("items"));
						for (int y = 0; y < itemsJSON.length(); y++) {
							Item item = JSONToVO.toItem(itemsJSON.getString(y));
							account.getItems().add(item);
						}
					} catch (Exception e) {
						Item item = JSONToVO.toItem(accountJson.getString("items"));
						account.getItems().add(item);
					}

				}

				accountList.add(account);
			}

			return accountList;

		} catch (JSONException e) {
			Log.i("ERROR", e.getMessage());
			try {
				accountResult = new JSONObject(result);
				JSONObject accountJson = accountResult.getJSONObject("account");
				Account account = new Account(accountJson.getInt("id"), accountJson.getString("client"), JSONToVO.toBoard(accountJson
						.getString("board")));

				accountList.add(account);
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			return accountList;
		}
	}

	public void insertAccount(Account account) {
		try {
			URL url = new URL(WebServiceUtil.URL + "accounts");

			HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
			conexao.setRequestMethod("POST");
			conexao.addRequestProperty("Content-type", "application/json");

			conexao.setDoOutput(true);

			conexao.connect();

			OutputStream os = conexao.getOutputStream();
			os.write(VOToJSON.toAccountJSON(account).toString().getBytes());
			os.flush();

			InputStream is = conexao.getInputStream();
			Log.i("RESTCLIENT POST", is.toString());

		} catch (IOException e) {
			Log.i("RESTCLIENT POST", e.getMessage());
		}
	}

	public void closeAccount(Integer id) {
		RestClient client = new RestClient();
		client.put(WebServiceUtil.URL + "accounts/" + id);
	}

}
